package com.jari.lg.service.profile.device;

import com.jari.lg.domain.CrossBase;
import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.DeverrRtEntity;
import com.jari.lg.mapper.DeverrRtEntityMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.Constants;
import com.jari.lg.utils.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class DeverrRtService {

    @Autowired
    private DeverrRtEntityMapper deverrRtEntityMapper;

    /*
    *  每10分钟入库 10条
    * */
    public void makeInfo(){
        DeverrRtEntity entity;
        List<DevBase> devBaseList = BaseData.devBaseList;
        List<CrossBase> crossBaseList = BaseData.crossBaseList;
        for(int i = 0; i < 10 ;i ++){
            entity = generate(devBaseList.get(Utils.makeRandInt(devBaseList.size())), crossBaseList);
            deverrRtEntityMapper.insert(entity);
        }
    }

    private DeverrRtEntity generate(DevBase devBase,List<CrossBase> crossBaseList){
        DeverrRtEntity entity = new DeverrRtEntity();
        entity.setDeviceid(devBase.getDevicecode());
        entity.setDevicename(devBase.getDevicename());
        entity.setDevicetype(devBase.getDevicetype());
        //如果不是安装在路口，就可能安装在大门
        if( StringUtils.isNotBlank( devBase.getKeyareagate())){
            entity.setCrossid(devBase.getKeyareagate());
            //找到大门的名称
            entity.setCrossname(crossBaseList.stream().filter(x -> x.getCrosscode().equals(devBase.getKeyareagate())).findFirst().get().getCrossname());
        }else if(StringUtils.isNotBlank(devBase.getCrosscode())){
            entity.setCrossname(entity.getCrossname());
            entity.setCrossid(entity.getCrossid());
        }
        entity.setDevicestate(Constants.DEVICE_ERR_STATUS);
        return  entity;
    }
}
